package com.ruix.app.widget.clip;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * User: Ray
 * Date: 15/10/10
 * ReadMe: 图片裁剪－移动缩放－自定义view－矩形裁剪框
 */
public class ClipRectImageBorderView extends View {

    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;
    /**
     * 垂直方向与View的边距
     */
    private int mVerticalPadding;
    /**
     * 绘制的矩形的宽度
     */
    private int mWidth;
    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 2;

    private Paint mPaint, mPaintRect;

    public ClipRectImageBorderView(Context context) {
        this(context, null);
    }

    public ClipRectImageBorderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClipRectImageBorderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mBorderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources().getDisplayMetrics());

        //绘制阴影
        mPaintRect = new Paint();
        mPaintRect.setARGB(185, 0, 0, 0);
        mPaintRect.setStyle(Style.FILL);

        // 绘制边框
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(mBorderWidth);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 计算矩形区域的宽度
        mWidth = getWidth() - 2 * mHorizontalPadding;
        // 计算距离屏幕垂直边界 的边距
        mVerticalPadding = (getHeight() - mWidth) / 2;

        // 绘制左边
        canvas.drawRect(0, 0, mHorizontalPadding, getHeight() - mVerticalPadding, mPaintRect);
        // 绘制右边
        canvas.drawRect(getWidth() - mHorizontalPadding, mVerticalPadding, getWidth(), getHeight(), mPaintRect);
        // 绘制上边
        canvas.drawRect(mHorizontalPadding, 0, getWidth(), mVerticalPadding, mPaintRect);
        // 绘制下边
        canvas.drawRect(0, getHeight() - mVerticalPadding, getWidth() - mHorizontalPadding, getHeight(), mPaintRect);

        //绘制边框
        canvas.drawRect(mHorizontalPadding, mVerticalPadding, mHorizontalPadding + mWidth, mVerticalPadding + mWidth, mPaint);

    }

    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;

    }

}
